package com.example.demo.controllers;

import com.example.demo.common.utils.ToolsUtil;
import com.example.demo.entity.KH;
import com.example.demo.services.KHService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ding XiJie
 *
 */
@RequestMapping("/kh")
@Controller
public class KHController {

    @Autowired
    private KHService khService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加客户
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(KH kh){
        try {
            kh = khService.save(kh);
            return ToolsUtil.returnMessage(0,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"添加失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 添加客户
     */
    @RequestMapping("/register")
    public String register(KH kh){
        try {
            kh = khService.save(kh);
            return "index.html";
        }catch (Exception e){
            e.printStackTrace();
            return "register.html";
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改客户
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(KH kh){
        try {
            kh = khService.update(kh);
            return ToolsUtil.returnMessage(0,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"修改失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除客户
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer kid){
        try {
            khService.delete(kid);
            return ToolsUtil.returnMessage(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"删除失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 查询所有客户
     */
    @RequestMapping("/getAllKh")
    @ResponseBody
    public Map<String, Object> getAllKh(Model model){
        Integer count = 0;
        try {
            List<KH> khList = khService.getAllKh();
            count = khService.findCount();
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 根据用户kid查询用户信息
     */
    @RequestMapping("/getKhByKid")
    @ResponseBody
    public Map<String, Object> getKhByKid(Integer kid){
        try {
            KH kh = khService.getKhByKid(kid);
            return ToolsUtil.returnTable(0,1,"查询成功",kh);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,0,"查询失败",null);
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toUserInfo")
    public String toUserInfo(Model model){
        return "user_info.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toUser")
    public String toUser(Model model){
        return "user.html";
    }


    /**
     * @Author: Ding XiJie
     * @Description: 跳转用户列表页面
     */
    @RequestMapping("/toKdgl")
    public String toKdgl(Model model){
        return "kdgl.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转用户列表页面
     */
    @RequestMapping("/toEdit")
    public String toEdit(Model model){
        return "kdgl_edit.html";
    }

}
